/**
 * 登录注册模块
 */
const express = require('express');
const router = express.Router();

const { networkStatus } = require('../utils/constant');
const { userLogin, userRegister } = require('../file/user');

// 用户登录
router.post('/login', async (req, res) => {
    
    try {
        const resData = await userLogin(req.body);
        console.log(resData);
        if( resData.code !== 200 ){
            res.send({
                code: resData.code,
                data: null,
                msg: resData.msg,
            })
            return;
        }
        // const token = await createToken(req.body);
        // 返回用户信息
        res.send({
            code: 200,
            data: resData.data,
            msg: resData.msg,
        })
    } catch (error) { 
        res.send({
            code: 500,
            data: error,
            msg: networkStatus['500'],
        })
    }
})

// 用户注册
router.post('/register', async (req, res) => {
    try {
        const resData = await userRegister(req.body);
        console.log(resData);
        if( resData.code !== 200 ){
            res.send({
                code: resData.code,
                data: null,
                msg: resData.msg,
            })
            return;
        }
        // const token = await createToken(req.body);
        // 返回用户信息
        res.send({
            code: 200,
            data: null,
            msg: resData.msg,
        })
    } catch (error) { 
        res.send({
            code: 500,
            data: error,
            msg: networkStatus['500'],
        })
    }
})

module.exports = router;